home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / Scripts / gifmaker.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  2.7 KB  |  136 lines

  1. #
  2. # The Python Imaging Library
  3. # $Id: gifmaker.py,v 1.1.1.1 1998/08/18 13:07:59 sjoerd Exp $
  4. #
  5. # convert sequence format to GIF animation
  6. #
  7. # history:
  8. #    97-01-03 fl    created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.  All rights reserved.
  11. # Copyright (c) Fredrik Lundh 1997.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16. #
  17. # For special purposes, you can import this module and call
  18. # the makedelta or compress functions yourself.  For example,
  19. # if you have an application that generates a sequence of
  20. # images, you can convert it to a GIF animation using some-
  21. # thing like the following code:
  22. #
  23. #    import Image
  24. #    import gifmaker
  25. #    
  26. #    sequence = []
  27. #    
  28. #    # generate sequence
  29. #    for i in range(100):
  30. #        im = <generate image i>
  31. #        sequence.append(im)
  32. #
  33. #    # write GIF animation
  34. #    fp = open("out.gif", "wb")
  35. #    gifmaker.makedelta(fp, sequence)
  36. #    fp.close()
  37. # Alternatively, use an iterator to generate the sequence, and
  38. # write data directly to a socket.  Or something...
  39. #
  40.  
  41. import Image, ImageChops
  42. import string
  43.  
  44. from GifImagePlugin import getheader, getdata
  45.  
  46. # --------------------------------------------------------------------
  47. # sequence iterator
  48.  
  49. class image_sequence:
  50.     def __init__(self, im):
  51.     self.im = im
  52.     def __getitem__(self, ix):
  53.     try:
  54.         if ix:
  55.         self.im.seek(ix)
  56.         return self.im
  57.     except EOFError:
  58.         raise IndexError # end of sequence
  59.  
  60. # --------------------------------------------------------------------
  61. # straightforward delta encoding
  62.  
  63. def makedelta(fp, sequence):
  64.     """Convert list of image frames to a GIF animation file"""
  65.  
  66.     frames = 0
  67.  
  68.     previous = None
  69.  
  70.     for im in sequence:
  71.  
  72.     #
  73.     # FIXME: write graphics control block before each frame
  74.  
  75.     if not previous:
  76.  
  77.         # global header
  78.         for s in getheader(im) + getdata(im):
  79.         fp.write(s)
  80.  
  81.     else:
  82.  
  83.         # delta frame
  84.         delta = ImageChops.subtract_modulo(im, previous)
  85.  
  86.         bbox = delta.getbbox()
  87.  
  88.         if bbox:
  89.  
  90.         # compress difference
  91.         for s in getdata(im.crop(bbox), offset = bbox[:2]):
  92.             fp.write(s)
  93.  
  94.         else:
  95.         # FIXME: what should we do in this case?
  96.         pass
  97.  
  98.     previous = im.copy()
  99.  
  100.     frames = frames + 1
  101.  
  102.     fp.write(";")
  103.  
  104.     return frames
  105.  
  106. # --------------------------------------------------------------------
  107. # main hack
  108.  
  109. def compress(infile, outfile):
  110.  
  111.     # open input image, and force loading of first frame
  112.     im = Image.open(infile)
  113.     im.load()
  114.  
  115.     # open output file
  116.     fp = open(outfile, "wb")
  117.  
  118.     seq = image_sequence(im)
  119.  
  120.     makedelta(fp, seq)
  121.  
  122.     fp.close()
  123.  
  124.  
  125. if __name__ == "__main__":
  126.  
  127.     import sys
  128.  
  129.     if len(sys.argv) < 3:
  130.     print "GIFMAKER -- create GIF animations"
  131.     print "Usage: gifmaker infile outfile"
  132.     sys.exit(1)
  133.  
  134.     compress(sys.argv[1], sys.argv[2])
  135.